package com.wandafilm.app.jetpack.frame.ui.film

import androidx.lifecycle.MutableLiveData
import com.kotlin.android.api.bean.SeatBanner
import com.kotlin.android.api.repository.SeatRepository
import com.kotlin.android.api.base.BaseUIModel
import com.kotlin.android.core.BaseViewModel

/**
 *
 * Created on 2020/4/21.
 *
 * @author o.s
 */
class SeatViewModel : BaseViewModel() {

    private val repo by lazy { SeatRepository() }
    private val _uiState = MutableLiveData<SeatUIModel>()
    private val _uiModel = SeatUIModel()
    val uiState: MutableLiveData<SeatUIModel>
        get() = _uiState

//    fun getSeatBanner() {
//        viewModelScope.launch {
//            emitUIState(showLoading = true)
//            val result = withOnIO {
//                repo.getSeatBanner()
//            }
//            withOnUI {
//                result.e()
//                when (result) {
//                    is ApiResult.Success -> {
//                        emitUIState(showLoading = false, success = result.data)
//                    }
//                    is ApiResult.Error -> emitUIState(error = result.error?.message)
//                }
//            }
//        }
//    }

//    fun getSeatBanner2(isRefresh: Boolean = true) {
//        viewModelScope.launch {
//            CallResult<SeatBanner>().refresh(isRefresh)
//                .loading {
//                    "loading".e()
//                    emitUIState(showLoading = true)
//                }.dismiss {
//                    "dismiss".e()
//                    emitUIState(showLoading = false)
//                }.success {
//                    "success :: $it".e()
//                    emitUIState(success = it)
//                }.error {
//                    "error :: $it".e()
//                    emitUIState(error = it)
//                }.call(errorMessage = "加载失败，请重试",
//                    successBlock = {
//
//                    }, errorBlock = {
//
//                    }, call = {
//                        "call".e()
//                        api.getSeatBanner()
//                    })
//        }
//    }

    private fun emitUIState(showLoading: Boolean = false) {
        _uiModel.showLoading = showLoading

        _uiState.value = _uiModel
    }

    private fun emitUIState(
        showLoading: Boolean = false,
        success: SeatBanner? = null,
        error: String? = null,
        loadMore: Boolean = false,
        isRefresh: Boolean = false
    ) {
        _uiModel.showLoading = showLoading
        _uiModel.success = success
        _uiModel.error = error
        _uiModel.loadMore = loadMore
        _uiModel.isRefresh = isRefresh

        _uiState.value = _uiModel
    }

    class SeatUIModel : BaseUIModel<SeatBanner>()
}